package com.dogdays.account.service.impl;

import com.dogdays.account.dao.UserDao;
import com.dogdays.account.dao.impl.UserDaoImpl;
import com.dogdays.account.generated.tables.pojos.User;
import com.dogdays.account.service.UserService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import org.jooq.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuban on 2017/3/23.
 */
@Service
public class UserServiceImpl implements UserService{

    private UserDao userDao;

    @Autowired
    private Vertx vertx;

    @Autowired
    private Configuration configuration;

    @PostConstruct
    public void init(){
        userDao = new UserDaoImpl(configuration,vertx);
    }

    @Override
    public UserService addUser(User user, Handler<AsyncResult<Void>> resultHandler) {
        userDao.insertAsync(user,resultHandler);
        return this;
    }

    @Override
    public UserService deleteUser(User user, Handler<AsyncResult<Void>> resultHandler) {
        List<User> userList = new ArrayList<>();
        userList.add(user);
        userDao.deleteAsync(userList,resultHandler);
        return this;
    }

    @Override
    public UserService updateUser(User user, Handler<AsyncResult<Void>> resultHandler) {
        userDao.updateAsync(user, resultHandler);
        return this;
    }

    @Override
    public UserService queryById(Long id, Handler<AsyncResult<User>> resultHandler) {
        userDao.fetchOneAsync(com.dogdays.account.generated.tables.User.USER.ID,id,resultHandler);
        return this;
    }
}
